package jwdt.xadmin.goods.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jwdt.common.bean.Tree;
import jwdt.common.dao.BaseDao;
import jwdt.xadmin.goods.bean.TGoodsClass;
import jwdt.xadmin.goods.service.GoodsClassManageService;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;

public class GoodsClassManageServiceImpl implements GoodsClassManageService {
	
	private BaseDao<TGoodsClass> baseDao;

	public void setBaseDao(BaseDao<TGoodsClass> baseDao) {
		this.baseDao = baseDao;
	}
	
	@Override
	public PageUtil selectGoodsClassList(PageUtil pu,int fid)throws Exception{
		String where = "fid=" + fid + " order by zt";
		pu.doList(TGoodsClass.class,where);
		return pu;
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,String>> allGoodsClass(int fid)throws Exception{
		String sql="select classid,name from t_goodsclass where fid='"+fid+"'";
		return Jdb.get().query(sql);
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,String>> allnoteGoodsClass(int fid)throws Exception{
		String sql="select classid,name from t_goodsclass where fid!='"+fid+"'";
		return Jdb.get().query(sql);
	}
	
	@Override
	public TGoodsClass selectOneGoodsClass(int classid)throws Exception{
		return baseDao.doLoadObj(TGoodsClass.class, classid);
	}
	
	@Override
	public String saveGoodsClass(int classid,TGoodsClass obj)throws Exception{
		if(classid==0){
			return baseDao.doAddObj(obj);
		}
		return baseDao.doUpdateObj(TGoodsClass.class, classid, obj);
	}
	
	@Override
	public String delGoodsClass(int classid)throws Exception{
		return baseDao.doDelObj(TGoodsClass.class, classid);
	}

	
	@SuppressWarnings("unchecked")
	public List<Tree> typeLeftTree()throws Exception{
		List<TGoodsClass> types = Jdb.get().query(TGoodsClass.class," fid=0 ORDER BY zt");
		List<Tree> list = new ArrayList<Tree>();
		Tree tree = new Tree();
		tree.setId("0");
		tree.setText("所有");
		tree.setChildren(treeList(types,null));
		list.add(tree);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Tree> treeList(List<TGoodsClass> types,List<Integer> quanxian)throws Exception{
		List<Tree> treeList = new ArrayList<Tree>();
		for(TGoodsClass type:types){
			Tree tree = new Tree();
			if(quanxian==null||quanxian.contains(type.getClassid())){
				tree.setId(type.getClassid()+"");
				tree.setText(type.getName());
				if(!type.getChildren().isEmpty()){
					tree.setChildren(treeList(type.getChildren(),quanxian));
				}
				treeList.add(tree);
			}
		}
		return treeList;
	}
	
}
